﻿using IntelligentDataCollection.BUS;
using IntelligentDataCollection.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO.Pipelines;

namespace IntelligentDataCollection.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeviceController : ControllerBase
    {
        public DeviceController()
        {
        }

        /// <summary>
        /// 查询网口数量
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryNetNum")]
        public ResponseMessage QueryNetNum()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                message.errcode = 0;
                message.data = Convert.ToInt32(AppSettingsHelper.Configuration["NetWorkNum"]);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 修改网口数量
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNetNum")]
        public ResponseMessage UpdateNetNum(int num)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {

                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["NetWorkNum"] = num;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        /// <summary>
        /// 查询网络信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryNetInfo")]
        public ResponseMessage QueryNetInfo(NetInfo netInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                NetInfo Net = new NetInfo();
                int networknum = Convert.ToInt32(AppSettingsHelper.Configuration["NetWorkNum"]);
                List<NetInfo> NetInfos = netBUS.GetNetInfo(networknum);
                List<NetInfo> NetInfo1 = NetInfos.FindAll(delegate (NetInfo e) { return e.ID == netInfo.ID; });
                if (NetInfo1 != null && NetInfo1.Count > 0)
                {
                    Net = NetInfo1[0];
                }
                else
                {
                    Net.ID = netInfo.ID;
                }
                message.errcode = 0;

                message.data = Net;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 修改网络信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNetInfo")]
        public ResponseMessage UpdateNetInfo(NetInfo netInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                int networknum = Convert.ToInt32(AppSettingsHelper.Configuration["NetWorkNum"]);
                bool result = netBUS.UpdateNetInfo(netInfo, networknum);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 设备重启
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("Reboot")]
        public ResponseMessage Reboot()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                netBUS.RunCommand("reboot");
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "设备重启异常";
            }

            return message;
        }

        /// <summary>
        /// 读取ntp服务器地址
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GetNtpIp")]
        public ResponseMessage GetNtpIp()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                string result = netBUS.GetNtpIp();

                message.errcode = 0;
                message.data = result;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询Ntp服务器地址异常";
            }

            return message;
        }

        /// <summary>
        /// 读取ntp服务器地址
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNtpIp")]
        public ResponseMessage UpdateNtpIp(string ip)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                bool result = netBUS.SetNtp(ip);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改Ntp服务器失败";
                }


            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询Ntp服务器地址异常";
            }

            return message;
        }

        /// <summary>
        /// 手动设置时间
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("CalibrationTime")]
        public ResponseMessage CalibrationTime(string time)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                bool result = netBUS.CalibrationTime(time);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "手动设置时间失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "手动设置时间异常";
            }

            return message;
        }
    }
}
